//
// Created by icezhang on 2021/9/8.
//

#ifndef DRON_TEST1_MQTTSINGLETON_H
#define DRON_TEST1_MQTTSINGLETON_H
#include "paho-mqtt/MQTTClient.h"

#define ADDRESS     "tcp://test.bodyta.com:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "cmqtt"
#define PAYLOAD     "Hello World!"
#define QOS         0
#define TIMEOUT     10000L


class MqttSingleton {

private:
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc;

    MqttSingleton(){
        MQTTClient_create(&client, ADDRESS, CLIENTID,
                          MQTTCLIENT_PERSISTENCE_NONE, NULL);
        conn_opts.keepAliveInterval = 20;
        conn_opts.cleansession = 1;

        if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
        {
            printf("Failed to connect, return code %d\n", rc);

        }
    }
    ~MqttSingleton(){
        MQTTClient_disconnect(client, 10000);
        MQTTClient_destroy(&client);

    }

public:
    static MqttSingleton & getMqttSingleton(){
        static MqttSingleton instance;
        return instance;
    }

    void doSomething();

};


#endif //DRON_TEST1_MQTTSINGLETON_H
